/**
 * class  BaseToggleButton
 * 可选按钮的基类，CheckBox,Radio继承与此类
 * author haicent
 * email haicent@126.com
 * version 0.01
 */
package as3.component.support
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;

	[Event(name="change", type="flash.events.Event")]

	public class BaseToggleButton extends BaseButton
	{
		protected var _selectDownState:DisplayObject;
		protected var _selectOverState:DisplayObject;
		protected var _selectUpState:DisplayObject;
		protected var _selectEnableState:DisplayObject;
		protected var _selectHitTestState:DisplayObject;
		
		//是否选中
		protected var _selected:Boolean = false;
		
		public function BaseToggleButton()
		{
			super();
		}
		
		override protected function initListener():void
		{
			this.addEventListener(MouseEvent.MOUSE_UP, $onMouseUpHandler);
		}
		
		public function set selected(value:Boolean):void
		{
			if (_selected == value) return;
			_selected = value;
			updateEnable();
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function changeSelect(value:Boolean):void
		{
			if (_selected == value) return;
			_selected = value;
			updateEnable();
		}
		
		public function set selectDownImage(dis:DisplayObject):void
		{
			if (dis && _selectDownState != dis)
			{
				_selectDownState = dis;
			}
		}
		
		public function set selectOverImage(dis:DisplayObject):void
		{
			if (dis && _selectOverState != dis) 
			{
				_selectOverState = dis;
			}
		}
		
		public function set selectUpImage(dis:DisplayObject):void
		{
			if (dis && _selectUpState != dis) 
			{
				_selectUpState = dis;
			}
		}
		
		public function set selectDisabledImage(dis:DisplayObject):void
		{
			if (dis && _selectEnableState != dis) 
			{
				_selectEnableState = dis;
			}
		}
		
		override public function set width(value:Number):void
		{
			if (_selectUpState) _selectUpState.width = value;
			if (_selectOverState) _selectOverState.width = value;
			if (_selectDownState) _selectDownState.width = value;
			if (_selectEnableState) _selectEnableState.width = value;
			super.width = value;
		}
		
		override public function set height(value:Number):void
		{
			if (_selectUpState) _selectUpState.height = value;
			if (_selectOverState) _selectOverState.height = value;
			if (_selectDownState) _selectDownState.height = value;
			if (_selectEnableState) _selectEnableState.height = value;
			super.height = value;
		}
		
		override protected function updateEnable():void
		{
			if (_enabled)
			{
				_simpleButton.upState = _selected ? _selectUpState : _upState;
				_simpleButton.overState = _selected ? _selectOverState : _overState;
				_simpleButton.downState = _selected ? _selectDownState : _downState;
				if (_selected)
				{
					if (_selectHitTestState) _simpleButton.hitTestState = _selectHitTestState;
					else {
						_simpleButton.hitTestState = _selectUpState;
					}
				} else {
					if (_hitTestState) _simpleButton.hitTestState = _hitTestState;
					else {
						_simpleButton.hitTestState = _upState;
					}
				}
				this.mouseChildren = true;
				this.mouseEnabled = true;
			} else {
				_simpleButton.upState = _selected ? _selectEnableState : _enableState;
				if (_selected)
				{
					if (_selectHitTestState) _simpleButton.hitTestState = _selectHitTestState;
					else {
						_simpleButton.hitTestState = _selectEnableState;
					}
				} else {
					if (_hitTestState) _simpleButton.hitTestState = _hitTestState;
					else {
						_simpleButton.hitTestState = _enableState;
					}
				}
				this.mouseEnabled = false;
				this.mouseChildren = false;
			}
		}
		
		protected function $onMouseUpHandler(evt:MouseEvent):void
		{
			selected = selected ? false : true;
		}
	}
}